<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="{{asset('lib/layui-v2.6.3/css/layui.css')}}" media="all">
    <link rel="stylesheet" href="{{asset('css/public.css')}}" media="all">
    <link rel="stylesheet" href="{{asset('css/app.css')}}" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">商品名称</label>
                            <div class="layui-input-inline">
                                <label>
                                    <input type="text" name="name" autocomplete="off" class="layui-input">
                                </label>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn-good"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo-goods">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add-goods"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId-goods" lay-filter="currentTableFilter-goods"></table>

        <script type="text/html" id="currentTableBar-goods">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit-good">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete-good">删除</a>
        </script>

    </div>
</div>

<script src="{{asset('lib/layui-v2.6.3/layui.js')}}" charset="utf-8"></script>
<script src="{{asset('lib/jquery-3.4.1/jquery-3.4.1.min.js')}}" charset="utf-8"></script>
<script>
        layui.use(['form', 'table'], function () {
        let $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#currentTableId-goods',
            url: '/api/goods/list',
            toolbar: '#toolbarDemo-goods',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [[
                {type: "checkbox", width: 40},
                {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'name', minWidth: 400, title: '商品名称'},
                {field: 'cate_id', width: 120, title: '所属分类', sort: true},
                {field: 'price', width: 120, title: '市场价'},
                {field: 'price_agent_one', width: 120, title: '一级代理价'},
                {field: 'price_agent_one', width: 120, title: '二级代理价'},
                {title: '操作', minWidth: 100, toolbar: '#currentTableBar-goods', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn-good)', function (data) {
            table.reload('currentTableId-goods', {
                url: '/api/goods/list',
                page: {
                    curr: 1
                },
                where: {
                    params: data.field
                }
            });

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter-goods)', function (obj) {
            if (obj.event === 'add-goods') {  // 监听添加操作
                var index = layer.open({
                    title: '添加商品',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/goods/create',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听批量删除操作
                var checkStatus = table.checkStatus('currentTableId-goods')
                    , data = checkStatus.data;
                if (data.length > 0) {
                    layer.confirm('确定删除吗？', function (index) {
                        let idArr = [];
                        for (let item of data) {
                            idArr.push(item.id);
                        }
                        let idStr = idArr.join(',');
                        //layer.alert(JSON.stringify(data));
                        //console.log(obj.data);
                        $.get({
                            dataType: 'text',
                            url: '/api/agent/delete?id=' + idStr,
                            success: function (status) {
                                //删除成功后刷新当前页
                                location.reload();
                            }
                        });
                    });
                }
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter-goods)', function (obj) {
            //console.log(obj)
        });

        table.on('tool(currentTableFilter-goods)', function (obj) {
            let data = obj.data;
            if (obj.event === 'edit-good') {
                let index = layer.open({
                    title: '编辑商品',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/goods/edit?id=' + data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete-good') {
                layer.confirm('确定删除吗？', function (index) {
                    $.get({
                        dataType: 'text',
                        url: '/api/goods/delete?id=' + obj.data.id,
                        success: function (status) {
                            obj.del();
                        }
                    });
                    layer.close(index);
                });
            }
        });

    });

</script>

</body>
</html>
